package data.FrameworkData;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import po.frameworkPO.FrameworkPO;
import dataservice.ResultMessage;
import dataservice.frameworkDataService.FrameworkDatabaseService;

public class FrameworkData extends UnicastRemoteObject implements
		FrameworkDatabaseService {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public FrameworkData() throws RemoteException {
		super();
	}

	@Override
	public ResultMessage update(FrameworkPO po) throws RemoteException {
		int result = -1;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection conn = DriverManager.getConnection(
					"jdbc:mysql://127.0.0.1:3306/选课管理系统", "root", "");
			Statement statement = conn.createStatement();
			String sqlString = "UPDATE `选课管理系统`.`framework` SET "
					+ "compulsoryLow='" + po.getCompulsoryLow() + "',"
					+ "compulsoryHigh='" + po.getCompulsoryHigh() + "',"
					+ "chooseLow='" + po.getChooseLow() + "'," + "chooseHigh='"
					+ po.getChooseHigh() + "'," + "coreLow='" + po.getCoreLow()
					+ "'," + "coreHigh='" + po.getCoreHigh() + "' ";
			System.out.println(sqlString);
			result = statement.executeUpdate(sqlString);
		} catch (SQLException e) {
			System.out.printf("ErrorCode is %d\n", e.getErrorCode());
			System.out.printf("ErrorMessage is %s\n", e.getMessage());
			if (e.getErrorCode() == 1452) {
				return ResultMessage.UPDATE_FOREIGNKEY_NOT_EXIST;
			}
			if (e.getErrorCode() == 1062) {
				return ResultMessage.UPDATE_DEPLICATE_PRIMARY;
			}
			if (e.getErrorCode() == 1048) {
				return ResultMessage.UPDATE_ISNULL;
			}
			if (e.getErrorCode() == 1406) {
				return ResultMessage.UPDATE_TOO_LONG;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (result == 1) {
			return ResultMessage.UPDATE_SUCCESS;
		} else {
			return ResultMessage.UPDATE_FAILURE;
		}
	}

	@Override
	public FrameworkPO find() throws RemoteException {
		int compulsoryLow = 0;
		int compulsoryHigh = 0;
		int chooseLow = 0;
		int chooseHigh = 0;
		int coreLow = 0;
		int coreHigh = 0;
		FrameworkPO frameworkPO = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection conn = DriverManager.getConnection(
					"jdbc:mysql://127.0.0.1:3306/选课管理系统", "root", "");
			Statement statement = conn.createStatement();
			String sqlString = "SELECT framework.* FROM framework ";
			System.out.println(sqlString);
			ResultSet rs = statement.executeQuery(sqlString);
			while (rs.next()) {
				compulsoryLow = rs.getInt(1);
				compulsoryHigh = rs.getInt(2);
				chooseLow = rs.getInt(3);
				chooseHigh = rs.getInt(4);
				coreLow = rs.getInt(5);
				coreHigh = rs.getInt(6);
				frameworkPO = new FrameworkPO(compulsoryLow, compulsoryHigh,
						chooseLow, chooseHigh, coreLow, coreHigh);
			}
		} catch (SQLException e) {
			System.out.printf("ErrorCode is %d\n", e.getErrorCode());
			System.out.printf("ErrorMessage is %s\n", e.getMessage());
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return frameworkPO;
	}

}